$(function () {
    //获取url
    var url = localStorage.getItem("ajaxUrl");
    var shop = localStorage.getItem("shop");
    var pass = 1;
    var upData = {};
    var describe = "";
    var tab = "tab1";

    $(".head h1").text((shop == "F01" ? "一期" : "二期") + "热处理日常盘点");

    $(".have").on("click", function () {
        tab = "tab1";
        $(".page .searchbar").show();
        $("#goods_id").attr("placeholder", "请扫描热处理标签").val("").focus();
        $("#tab1 input").val("");
        $("#tab1 select").val("").trigger("change");
    });
    $(".nohave").on("click", function () {
        tab = "tab2";
        //$(".page .searchbar").hide();
        $("#goods_id").attr("placeholder","请扫描或输入排产单").val("").focus();
        $("#tab2 input").val("");
        $("#tab2 select").val("").trigger("change");
    });

    // 获取 热处理 工作中心
    loading_show("获取中...");
    $.ajax({
        headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
        type: "post",
        url: url + '/app/init/getHeatMachine',
        data: {
            shop: shop,
        },
        dataType: 'json',
        json: 'callback',
        success: function (res) {
            pass = 1;
            if (res.success) {
                loading_hide();
                var data = res.result;
                var machineHtml = "<option value=''>请选择工作中心</option>";
                for (var i = 0; i < data.length; i++) {
                    machineHtml += '<option value="' + data[i].machineCode + '">' + data[i].machine + '</option>';
                }
                $("#tab2 select.machine").html(machineHtml);
            } else {
                $alert(res.message);
                $("input.id").val("");
            }
        },
        error: function (error) {
            pass = 1;
            //加载提示符 隐藏
            loading_hide();
            console.log(error);
            $alert(error.responseText);
        }
    });

    //批号   获取 焦点
    $("input#goods_id").focus();

    //点击返回箭头 清除 缓存 打开 storekeeper.html
    $(".head .goBack").on("click", function () {
        localStorage.removeItem("shop");
        window.location.href = "dailyinit.html";
    });

    $(".popup-addGoods").on("click", ".head .pull-left", function () {
        $(".content ul.ul_supplier").empty();
        $.closeModal(".popup-addGoods");
    });

    //根据扫描内容 得到 热处理信息
    $("#goods_id").on("keydown", function (event) {
        if (event.keyCode == "13") {
            if (pass == 0) {
                return
            }
            pass = 0;
            var val = $(this).val();
            //$("#goods_id").blur();
            if (!val) {
                $alert("请扫描！");
                pass = 1;
                setTimeout(function () {
                    $("#goods_id").val("").focus();
                }, 1400);
                return;
            }
            //加载提示符 显示
            if (tab == "tab1") {
                loading_show("搜索中...");
                //获取工单
                $.ajax({
                    headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                    type: "post",
                    url: url + '/app/init/getHeatwarehouse_daily',
                    data: {
                        shop: shop,
                        batch: val
                    },
                    dataType: 'json',
                    json: 'callback',
                    success: function (res) {
                        pass = 1;
                        if (res.success) {
                            //加载提示符 隐藏
                            loading_hide();
                            var data = res.result;
                            $("#goods_id").val("");
                            //console.log(data);
                            describe = val;
                            $("#tab1 input.batch").val(data.batch);
                            $("#tab1 input.gg").val(data.inventorySpecification);
                            $("#tab1 input.pm").val(data.inventoryName);
                            $("#tab1 input.mz").val(data.grossQuantity);
                            $("#tab1 input.pz").val(data.tareQuantity);
                        } else {
                            $("#goods_id").val("");
                            $alert(res.message);
                        }
                    },
                    error: function (error) {
                        pass = 1;
                        //加载提示符 隐藏
                        loading_hide();
                        console.log(error);
                        setTimeout(function () {
                            $("#goods_id").val("").focus();
                        }, 1400);
                        $alert(error.responseText);
                    }
                });
            } else {
                loading_show("搜索中...");
                //获取工单
                $.ajax({
                    headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                    type: "post",
                    url: url + '/app/init/getHeatWorkplanNumber_daily',
                    data: {
                        shop: shop,
                        workplanNumber: val
                    },
                    dataType: 'json',
                    json: 'callback',
                    success: function (res) {
                        pass = 1;
                        //加载提示符 隐藏
                        loading_hide();
                        if (res.success) {
                            var data = res.result;
                            $("#goods_id").val("");
                            //console.log(data);
                            var ggHtml = "<option>请选择工序规格</option>";
                            for (var i = 0; i < data.length; i++) {
                                ggHtml += '<option value="' + data[i].inventoryNameSpecification + '">' + data[i].inventoryNameSpecification + '</option>';
                            }
                            $("#tab2 select.gg").html(ggHtml);
                            if (data.length == 1) {
                                $("#tab2 select.gg").val(data[0].inventoryNameSpecification).trigger("change");
                            }
                            $("#tab2 input.workplanNumber").val(data[0].workplanNumber);
                            $("#tab2 input.mz").val("");
                            $("#tab2 input.pz").val("");
                        } else {
                            $("#goods_id").val("");
                            $alert(res.message);
                        }
                    },
                    error: function (error) {
                        pass = 1;
                        //加载提示符 隐藏
                        loading_hide();
                        console.log(error);
                        setTimeout(function () {
                            $("#goods_id").val("").focus();
                        }, 1400);
                        $alert(error.responseText);
                    }
                });
            }
        }
    });

    //点击 获取编号
    $(".foot").on("click", ".del_receive", function () {
        if (pass == 0) {
            return
        }
        pass = 0;
        $.ajax({
            headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
            type: "post",
            url: url + '/app/init/getHeatNumber',
            data: {},
            dataType: 'json',
            json: 'callback',
            cache: false,
            success: function (res) {
                pass = 1;
                if (res.success) {
                    var data = res.result;
                    $alert("获取成功", 800);
                    $("#tab2 input.batch").val(data.number);
                } else {
                    $alert(res.message);
                }
            },
            error: function (error) {
                pass = 1;
                console.log(error);
                $alert(error.responseText);
            }
        });
    });

    //点击 确认
    $(".foot").on("click", ".add_receive", function () {
        if (pass == 0) {
            return
        }
        pass = 0;
        if (tab == "tab1") {
            if (!$("#tab1 input.batch").val()) {
                pass = 1;
                $alert("编号不可为空！");
                return;
            }
            loading_show("入库中...");
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "post",
                url: url + '/app/init/initHeatwarehouse_daily',
                data: {
                    type: "有标签",
                    shop: shop,
                    batch: $("#tab1 input.batch").val()
                },
                dataType: "json",
                json: "callback",
                timeout: 60000,
                cache: false,
                success: function (res) {
                    pass = 1;
                    loading_hide();
                    if (res.success) {
                        $alert($("#tab1 input.batch").val() + "盘点成功");
                        $("#tab1 input").val("");
                        $("#tab1 select").val("").trigger("change");
                    } else {
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    pass = 1;
                    if (error.status == 0) {
                        loading_hide();
                        $alert("请求超时，请重新提交！");
                    } else {
                        loading_hide();
                        console.log(error);
                        $alert(error.responseText);
                    }
                }
            });
        } else {
            //判断数据
            if (!$("#tab2 input.workplanNumber").val()) {
                pass = 1;
                $alert("排产单不可为空！");
                return;
            } else if (!$("#tab2 input.batch").val()) {
                pass = 1;
                $alert("编号不可为空！");
                return;
            } else if (!$("#tab2 select.gg").val()) {
                pass = 1;
                $alert("规格不可为空！");
                return;
            } else if (!$("#tab2 input.mz").val()) {
                pass = 1;
                $alert("毛重不可为空！");
                return;
            } else if (Number($("#tab2 input.mz").val()) <= 0) {
                pass = 1;
                $alert("毛重应大于0！");
                return;
            } else if (!$("#tab2 input.pz").val()) {
                $alert("皮重不可为空！");
                return;
            } else if (Number($("#tab2 input.pz").val()) < 0) {
                pass = 1;
                $alert("皮重应大于0！");
                return;
            } else if (Number(accSub($("#tab2 input.mz").val(), $("#tab2 input.pz").val()) <= 0)) {
                pass = 1;
                $alert("毛重应大于皮重！");
                return;
            } else if (!$("#tab2 select.machine").val()) {
                pass = 1;
                $alert("产线不可为空！");
                return;
            }
            //获取 信息
            var workplanNumber = $(".content #tab2 .workplanNumber").val();
            var batch = $(".content #tab2 .batch").val();
            var gg = $(".content #tab2 .gg").val();
            var mz = $(".content #tab2 .mz").val();
            var pz = $(".content #tab2 .pz").val();
            var machine = $(".content #tab2 .machine").val();
            loading_show("入库中...");
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "post",
                url: url + '/app/init/initHeatwarehouse_daily',
                data: {
                    type: "无标签",
                    shop: shop,
                    workplanNumber: workplanNumber,
                    batch: batch,
                    specification: gg,
                    mz: mz,
                    pz: pz,
                    machine: machine,
                },
                dataType: "json",
                json: "callback",
                timeout: 60000,
                cache: false,
                success: function (res) {
                    pass = 1;
                    loading_hide();
                    if (res.success) {
                        $alert(batch + "盘点成功");
                        $(".content #tab2 .batch").val("");
                        $(".content .pz").val("");
                        $(".content .mz").val("0");
                        upData = res.result;
                        takeupPrint("heatTakeup", res.result);
                    } else {
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    pass = 1;
                    if (error.status == 0) {
                        loading_hide();
                        $alert("请求超时，请重新提交！");
                    } else {
                        loading_hide();
                        console.log(error);
                        $alert(error.responseText);
                    }
                }
            });
        }
    });

    //选中 某一个 数据  之后
    $(".div_inventory_ul").on("click", "li", function () {
        var data = $(this).text();
        var code = $(this).attr("data-code");
        var title = $(this).parent().prev().text();
        // console.log(data);
        // console.log(code);
        if (title.indexOf("规格") >= 0) {
            $("#tab2 input.gg").val(data.split(',')[1]);
            $("#tab2 input.gg").attr("data-code", code);
            $.closeModal('.popup-about');
            $alert("规格选择成功！");
        } else if (title.indexOf("客户") >= 0) {
            $("#tab2 input.customer").val(data);
            $("#tab2 input.customer").attr("data-code", code);
            $.closeModal('.popup-about');
            $alert("客户选择成功！");
        }
    });

    //点击 关闭，清除输入框中数据
    $(".icon-guanbi").click(function () {
        $(this).hide().parent().parent().find("input").val("").focus();
        $(this).hide().parent().parent().find("input").attr("data-code", "");
    });

    //重打
    $(".head .icon-dayin").on("click", function () {
        //console.log(upData)
        if (upData.batch) {
            $alert(upData.batch + "打印成功");
            takeupPrint("heatTakeup", upData);
        } else {
            $alert("暂无数据");
        }
    });

    //皮重 毛重 失去 焦点 则 计算 净重
    // $(".content").on("input", "input.pz", function () {
    //     setCpmz();
    // });
    // $(".content").on("input", "input.jz", function () {
    //     setCpmz();
    // });
    //
    // //计算 毛重
    // function setCpmz() {
    //     var jz = $(".content input.jz").val();
    //     var pz = $(".content input.pz").val();
    //     if (jz == "") {
    //         return;
    //     } else {
    //         if (pz == "") {
    //             pz = 0;
    //         }
    //         $(".content input.mz").val(accAdd(jz, pz));
    //     }
    // }

});

